﻿using System.Windows.Forms;
using System;
using System.Collections.Generic;

namespace MutsiEngine
{
    public class UserInput
    {
        private HashSet<Keys> pressedKeyList = new HashSet<Keys>();
        private Form form;

        public Vector2 MousePosition
        {
            get { return new Vector2(SharpGL.SceneControl.MousePosition.X, SharpGL.SceneControl.MousePosition.Y); }
        }
        public bool MouseLeftButtonPressed
        {
            get { return SharpGL.SceneControl.MouseButtons == System.Windows.Forms.MouseButtons.Left; }
        }
        public bool MouseRightButtonPressed
        {
            get { return SharpGL.SceneControl.MouseButtons == System.Windows.Forms.MouseButtons.Right; }
        }
        public bool MouseMiddleButtonPressed
        {
            get { return SharpGL.SceneControl.MouseButtons == System.Windows.Forms.MouseButtons.Middle; }
        }

        public UserInput(Form form)
        {
            this.form = form;
            form.Controls.Add(new KeyControl(form, keyDownHandler, keyUpHandler));
        }

        public void Update()
        {
            if (!form.Controls["keyControl"].Focused && form.Controls["keyControl"].CanFocus)
            {
                form.Controls["keyControl"].Focus();
            }
        }

        public bool IsKeyDown(Keys key)
        {
            return pressedKeyList.Contains(key);
        }

        private void keyDownHandler(object sender, KeyEventArgs e)
        {
            pressedKeyList.Add(e.KeyCode);
            e.Handled = true;
        }

        private void keyUpHandler(object sender, KeyEventArgs e)
        {
            pressedKeyList.Remove(e.KeyCode);
            e.Handled = true;
        }
    }

    public class KeyControl : Control
    {
        public KeyControl(Form form, KeyEventHandler keyDownHandler, KeyEventHandler keyUpHandler)
        {
            this.Name = "KeyControl";
            this.KeyDown += keyDownHandler;
            this.KeyUp += keyUpHandler;
        }

        protected override bool IsInputKey(Keys keyData)
        {
            return true;
        }
    }
}


